Completed
Push — master ( 8967bc...e88c19 )
by Rain
02:47
created

Momentor.js ➔ format   D

Complexity

Conditions 12
Paths 160

Size

Total Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 12
c 1
b 0
f 0
nc 160
nop 2
dl 0
loc 40
rs 4.8484

How to fix   Complexity   

Complexity

Complex classes like Momentor.js ➔ format often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
2
import window from 'window';
3
import _ from '_';
4
import $ from '$';
5
import moment from 'moment';
6
import {i18n} from 'Common/Translator';
7
8
let _moment = null;
9
let _momentNow = 0;
10
11
const updateMomentNow = _.debounce(() => {
12
	_moment = moment();
13
}, 500, true);
14
15
const updateMomentNowUnix = _.debounce(() => {
16
	_momentNow = moment().unix();
17
}, 500, true);
18
19
/**
20
 * @returns {moment}
21
 */
22
export function momentNow()
23
{
24
	updateMomentNow();
25
	return _moment || moment();
26
}
27
28
/**
29
 * @returns {number}
30
 */
31
export function momentNowUnix()
32
{
33
	updateMomentNowUnix();
34
	return _momentNow || 0;
35
}
36
37
/**
38
 * @param {number} date
39
 * @returns {string}
40
 */
41
export function searchSubtractFormatDateHelper(date)
42
{
43
	return momentNow().clone().subtract(date, 'days').format('YYYY.MM.DD');
44
}
45
46
/**
47
 * @param {Object} m
48
 * @returns {string}
49
 */
50
function formatCustomShortDate(m)
51
{
52
	const now = momentNow();
53
	if (m && now)
54
	{
55
		switch (true)
56
		{
57
			case 4 >= now.diff(m, 'hours'):
58
				return m.fromNow();
59
			case now.format('L') === m.format('L'):
60
				return i18n('MESSAGE_LIST/TODAY_AT', {
61
					TIME: m.format('LT')
62
				});
63
			case now.clone().subtract(1, 'days').format('L') === m.format('L'):
64
				return i18n('MESSAGE_LIST/YESTERDAY_AT', {
65
					TIME: m.format('LT')
66
				});
67
			case now.year() === m.year():
68
				return m.format('D MMM.');
69
			// no default
70
		}
71
	}
72
73
	return m ? m.format('LL') : '';
74
}
75
76
/**
77
 * @param {number} timeStampInUTC
78
 * @param {string} formatStr
79
 * @returns {string}
80
 */
81
export function format(timeStampInUTC, formatStr)
82
{
83
84
	let
85
		m = null,
0 ignored issues
show
Unused Code introduced by
The assignment to m seems to be never used. If you intend to free memory here, this is not necessary since the variable leaves the scope anyway.
Loading history...
86
		result = '';
87
88
	const now = momentNowUnix();
89
90
	timeStampInUTC = 0 < timeStampInUTC ? timeStampInUTC : (0 === timeStampInUTC ? now : 0);
91
	timeStampInUTC = now < timeStampInUTC ? now : timeStampInUTC;
92
93
	m = 0 < timeStampInUTC ? moment.unix(timeStampInUTC) : null;
94
95
	if (m && 1970 === m.year())
96
	{
97
		m = null;
98
	}
99
100
	if (m)
101
	{
102
		switch (formatStr)
103
		{
104
			case 'FROMNOW':
105
				result = m.fromNow();
106
				break;
107
			case 'SHORT':
108
				result = formatCustomShortDate(m);
109
				break;
110
			case 'FULL':
111
				result = m.format('LLL');
112
				break;
113
			default:
114
				result = m.format(formatStr);
115
				break;
116
		}
117
	}
118
119
	return result;
120
}
121
122
/**
123
 * @param {Object} element
124
 * @returns {void}
125
 */
126
export function momentToNode(element)
127
{
128
	var
129
		key = '',
130
		time = 0,
131
		$el = $(element);
132
133
	time = $el.data('moment-time');
134
	if (time)
135
	{
136
		key = $el.data('moment-format');
137
		if (key)
138
		{
139
			$el.text(format(time, key));
140
		}
141
142
		key = $el.data('moment-format-title');
143
		if (key)
144
		{
145
			$el.attr('title', format(time, key));
146
		}
147
	}
148
}
149
150
/**
151
 * @returns {void}
152
 */
153
export function reload()
154
{
155
	_.defer(() => {
156
		$('.moment', window.document).each((index, item) => {
157
			momentToNode(item);
158
		});
159
	});
160
}
161